﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CrystalDecisions.Enterprise;


namespace BO.Management
{
    public class SecurityInfoProvider
    {

        public const String EVERYONE = "Everyone";
        public const Int32 MAXINSTANCEPEROBJECTID = 1360;

        /// <summary>
        /// Initializes a new instance of the SecurityInfoProvider class.
        /// </summary>
        public SecurityInfoProvider()
        {
        }

        public SecurityInfo GetSecurityInfo(InfoObject boeObject)
        {
            if (boeObject == null) throw new ArgumentNullException("boeObject");
            return boeObject.SecurityInfo;
        }


        public SecurityLimits GetKnownSecurityLimit(InfoObject boeObject)
        {
            SecurityInfo secInfo = this.GetSecurityInfo(boeObject);
            return secInfo.KnownLimits;
        }

        public ObjectPrincipals GetObjectPrincipals(InfoObject boeObject)
        {
            SecurityInfo secInfo = this.GetSecurityInfo(boeObject);
            return secInfo.ObjectPrincipals;
        }

        private ObjectPrincipal GetObjectPrincipal(InfoObject boeObject,String PrincipalName)
        {
            SecurityInfo secInfo = this.GetSecurityInfo(boeObject);
            return secInfo.ObjectPrincipals[PrincipalName];
        }

        public SecurityLimit GetSecurityLimit(InfoObject boeObject, String PrincipalName)
        {
            ObjectPrincipal principal = this.GetObjectPrincipal(boeObject,PrincipalName);
            if (principal.Limits.Count == 0)
                return null;
            else
                return  principal.Limits[1];
        }

        public SecurityLimit GetDefaultLimit(InfoObject boeObject)
        {
            return this.GetSecurityLimit(boeObject, EVERYONE);
        }

        public SecurityLimits GetDefaultLimitContainer(InfoObject boeObject)
        {
            ObjectPrincipal principal = this.GetObjectPrincipal(boeObject, EVERYONE);
            return principal.Limits;
        }


    }
}